package com.itdianzi.controller.print;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itdianzi.controller.base.BaseController;
import com.itdianzi.domain.cargo.ContractProduct;
import com.itdianzi.service.cargo.ContractProductService;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/cargo/contractProduct")
public class ContractProductImportController extends BaseController {

    @Reference
    private ContractProductService contractProductService;

    @RequestMapping("/toImport")
    public String toImport(String contractId) {

        request.setAttribute("contractId", contractId);
        return "cargo/product/product-import";
    }

    @RequestMapping("/import")
    public String importProduct(String contractId, MultipartFile file) throws Exception {

        //构
        // 建list对象
        List<ContractProduct> contractProductList = new ArrayList<>();
        //工作簿对象
        Workbook wb = new XSSFWorkbook(file.getInputStream());
        //读取表
        Sheet sheet = wb.getSheetAt(0);
        //读取行
        //最后一行
        int lastRowNum = sheet.getLastRowNum();
        Row row = null;
        //提前设定一个数组
        Object[] params = new Object[10];

        //循环遍历
        for (int i = 1; i <= lastRowNum; i++) {
            row = sheet.getRow(i);//获取每一行
            short lastCellNum = row.getLastCellNum();//最后一列
            //循环，一次走完整个数组
            for (int j=1;j<lastCellNum;j++) {
                Cell cell = row.getCell(j);//获得单元格
                params[j] = getValue(cell);//将excel的数据转换为数组
            }
            //直接将数组封装成对象
            ContractProduct contractProduct = new ContractProduct(params, super.companyId, companyName, contractId);
            //将对象添加到集合中
            contractProductList.add(contractProduct);
        }

        //读取列，存入数据库，有事物问题
        contractProductService.saveList(contractProductList);


        return "redirect:/cargo/contract/list.do";

    }


    /**
     * 传入cell对象 根据不同单元格数据类型 获得数据 并且返回Object类型
     * @param cell
     * @return
     */
    private static Object getValue(Cell cell) {

        Object o = null;
        //拿到类型
        CellType cellType = cell.getCellType();
        //判断类型
        switch (cellType){
            case STRING:
                o = cell.getStringCellValue();
                break;
            case NUMERIC: //在excel中 数字类型 和 日期类型 类型一致 , 在java中不一致
                boolean flag = DateUtil.isCellDateFormatted(cell); //判断当前单元格是不是日期类型
                if(flag){
                    o= cell.getDateCellValue(); //获得日期类型
                }else{
                    o=cell.getNumericCellValue();//获得数字类型
                }
                break;
            case BOOLEAN:
                o=cell.getBooleanCellValue();
                break;
            default:
                break;
        }
        return o;
    }


}
